<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
load()->func('tpl');
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$where=' WHERE  uniacid=:uniacid  ';
$data[':uniacid']=$_W['uniacid'];
if($_GPC['time']){
    $start=strtotime($_GPC['time']['start']);
    $end=strtotime($_GPC['time']['end']);
    $where.=" and timesjc >={$start} and timesjc<={$end}";
}
if(isset($_GPC['keywords'])){
    $where.="  and (ordernum LIKE  concat('%', :name,'%') or uname LIKE  concat('%', :name,'%') or phone LIKE  concat('%', :name,'%'))";
    $data[':name']=$_GPC['keywords']; 
}
if($_GPC['type']){
  if ($_GPC['state']) {
  	$where.=" and type={$_GPC['type']} and state={$_GPC['state']}";
	$active=$_GPC['type'];
	$activef=$_GPC['state'];
  }else{
  	$where.=" and type={$_GPC['type']}";
	$active=$_GPC['type'];
  }
}else{
  if ($_GPC['state']) {
  	$where.=" and state={$_GPC['state']}";
	$active=0;
	$activef=$_GPC['state'];
  }
}
// if ($_GPC['type']) {
// 	$sql="SELECT * FROM ".tablename('zh_gjw_orders') .$where."AND type=".$_GPC['type']." ORDER BY time desc";
// 	$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_orders').$where."AND type=".$_GPC['type']." ORDER BY time desc",$data);
// 	$active=$_GPC['type'];
// }elseif ($_GPC['state']) {
// 	$sql="SELECT * FROM ".tablename('zh_gjw_orders') .$where."AND state=".$_GPC['state']." ORDER BY time desc";
// 	$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_orders').$where."AND state=".$_GPC['state']." ORDER BY time desc",$data);
// 	$activef=$_GPC['state'];
// }else{
	$sql="SELECT * FROM ".tablename('zh_gjw_orders') .$where." ORDER BY time desc";
	$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_orders').$where." ORDER BY time desc",$data);
// }
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$orders=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);

if($operation=='delete'){
	$res=pdo_delete('zh_gjw_orders',array('id'=>$_GPC['id']));
	// var_dump($res);die;
	if($res){
		pdo_delete('zh_gjw_orderassess',array('p_id'=>$_GPC['id']));
	  	message('删除成功',$this->createWebUrl('orderlist',array()),'success');
	}else{
	  message('删除失败','','error');
	}
}
if($_GPC['op']=='three'){
	$now=strtotime('now');
	$end=strtotime("+10 days");
	// $end=strtotime("+10 seconds");
	$res=pdo_update('zh_gjw_orders',array('stime'=>$now,'etime'=>$end,'state'=>3),array('id'=>$_GPC['id']));
	if($res){
     message('', 'referer', 'success');
		// iajax('200','成功',$this->createWebUrl('activebanner'), 'ajax', false);
    }else{
          message('失败！','referer','error');
    }
}
if($_GPC['op']=='four'){
  	function getLevel($members, $xf_money)
    {
        for ($i = 0; $i < count($members); $i++) {
            if ($xf_money >= $members[$i]['money']) {
                return $members[$i]['name'];
            }
            if ($xf_money < $members[$i]['money'] && $xf_money > $members[$i + 1]['money']) {
                return $members[$i + 1]['name'];
            }
        }
    }
	$res=pdo_update('zh_gjw_orders',array('state'=>4),array('id'=>$_GPC['id']));
	if($res){
			$cus=pdo_get('zh_gjw_orders',array('id'=>$_GPC['id']));
			$totalmoney=pdo_get('zh_gjw_orders', array('cus_id' => $cus['cus_id'], 'uniacid' => $_W['uniacid'],'state'=>4), array('sum(ofees) as total_money'));
	        $store = pdo_getall('zh_gjw_storegrade', array('uniacid' => $_W['uniacid']), array(), '', 'money DESC');
	        $storename = getLevel($store, $totalmoney['total_money']);
	        if ($storename) {
	            pdo_update('zh_gjw_cusmanage', array('level_name' => $storename), array('id' => $cus['cus_id']));
	        }
     message('', 'referer', 'success');
		// iajax('200','成功',$this->createWebUrl('activebanner'), 'ajax', false);
    }else{
          message('失败！','referer','error');
    }
}
if(checksubmit('submit2')){
		function set_msg($_W,$id){
				  $orderid=pdo_get('zh_gjw_orders',array('uniacid'=>$_W['uniacid'],'id'=>$id));
				  if ($orderid['excompany']=='undefined' || $orderid['excompany']==null) {
				  	$orderid['excompany']=$orderid['excompanys'];
				  }
				  	if ($orderid['type']==1) {
						$dbimg=pdo_get('zh_gjw_dbpublish',array('id'=>$orderid['p_id']));
						$orderid['title']=$dbimg['dbname'];
					}
					if ($orderid['type']==2) {
						$shotimg=pdo_get('zh_gjw_shotpublish',array('id'=>$orderid['p_id']));
						$orderid['title']=$shotimg['title'];
					}
					if ($orderid['type']==3) {
						$marketimg=pdo_get('zh_gjw_marketpublish',array('id'=>$orderid['p_id']));
						$orderid['title']=$marketimg['title'];
					}
					if ($orderid['type']==4) {
						$storeimg=pdo_get('zh_gjw_storepublish',array('id'=>$orderid['p_id']));
						$orderid['title']=$storeimg['title'];
					}
				  $openid_id=pdo_get('zh_gjw_register',array('uniacid'=>$_W['uniacid'],'id'=>$orderid['u_id']));
				  $cusname=pdo_get('zh_gjw_cusmanage',array('uniacid'=>$_W['uniacid'],'openid'=>$openid_id['openid']));
                  $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                  $formwork ='{
                      "touser": "'.$openid_id["openid"].'",
                      "template_id": "'.$ress["templeidoo"].'",
                      "form_id":"'.$orderid['formid'].'",
                      "data": {
                          "keyword1": {
                              "value": "'.$orderid["excompany"].'",
                              "color": "#173177"
                          },
                          "keyword2": {
                              "value":"'.$orderid["extime"].'",
                              "color": "#173177"
                          },
                          "keyword3": {
                              "value":"'.$orderid["paytime"].'",
                              "color": "#173177"
                          },
                          "keyword4": {
                              "value":"'.$orderid["title"].'",
                              "color": "#173177"
                          },
                          "keyword5": {
                              "value":"'.$cusname["cusname"].'",
                              "color": "#173177"
                          },
                          "keyword6": {
                              "value":"'.$orderid["ordernum"].'",
                              "color": "#173177"
                          },
                          "keyword7": {
                              "value":"'.$orderid["id"].'",
                              "color": "#173177"
                          },
                          "keyword8": {
                              "value":"'.$orderid["exordernums"].'",
                              "color": "#173177"
                          },
                          "keyword9": {
                              "value":"'.$orderid["exordernum"].'",
                              "color": "#173177"
                          },
                          "keyword10": {
                              "value":"已发货(“快递100小助手”小程序中查询)",
                              "color": "#173177"
                          }
                      }  
                  }';
                  $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$ress['accessToken']."";
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL,$url);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                  curl_setopt($ch, CURLOPT_POST,1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
                  $data = curl_exec($ch);
                  curl_close($ch);
                 return $data;
            }
        $now=strtotime('now');
		$end=strtotime("+10 days");
		$res=pdo_update('zh_gjw_orders',array('stime'=>$now,'etime'=>$end,'state'=>3,'excompany'=>$_GPC['excompany'],'exordernum'=>$_GPC['exordernum'],'excompanys'=>$_GPC['excompanys'],'exordernums'=>$_GPC['exordernums']),array('id'=>$_GPC['id']));
		if($res){
            set_msg($_W,$_GPC['id']);
	     message('', 'referer', 'success');
	    }else{
	          message('失败！','referer','error');
	    }
}
include $this->template('web/orderlist');